package com.doudizu.shoesonline.controller;

import com.doudizu.shoesonline.annotation.AuthCheck;
import com.doudizu.shoesonline.common.BaseResponse;
import com.doudizu.shoesonline.common.ResultUtils;
import com.doudizu.shoesonline.model.dto.product.ProductDTO;
import com.doudizu.shoesonline.model.dto.product.ProductQuery;
import com.doudizu.shoesonline.model.entity.Product;
import com.doudizu.shoesonline.model.vo.ProductDetailVO;
import com.doudizu.shoesonline.model.vo.ProductVO;
import com.doudizu.shoesonline.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ln
 */
@RestController
@RequestMapping("/product")
@Api(tags = "产品模块")
public class ProductController {

    @Resource
    private ProductService productService;


    @PostMapping("/recommend")
    @ApiOperation(value = "推荐",notes = "按照销量降序")
    public BaseResponse recommend(Integer pageNum, Integer pageSize) {
        Object obj = productService.recommend(pageNum, pageSize);
        return ResultUtils.success(obj);
    }

    @PostMapping("/recommend2")
    @ApiOperation(value = "根据订单推荐",notes = "根据订单推荐")
    public BaseResponse recommend() {
        Object obj = productService.recommend();
        return ResultUtils.success(obj);
    }

    @GetMapping
    @ApiOperation("获取单个产品详细信息")
    public BaseResponse getOne(Integer id) {
        ProductDetailVO product = productService.getProductDetail(id);
        return ResultUtils.success(product);
    }

    /**
     * @param productQuery
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "(分页)(条件)查询", notes = "传pageNum、pageSize则分页,不传不分页,不传条件查全部")
    public BaseResponse getByCondition(ProductQuery productQuery) {
        Object obj = productService.condition(productQuery);
        return ResultUtils.success(obj);
    }

    @PostMapping("/add")
    @ApiOperation("添加产品")
    @AuthCheck(mustRole = "admin")
    public BaseResponse add(@RequestBody ProductDTO productDTO) {
        productService.addProduct(productDTO);
        return ResultUtils.success(null);
    }

    @GetMapping("/search")
    @ApiOperation("模糊搜索")
    public BaseResponse search(String text) {
        ProductQuery productQuery = new ProductQuery();
        if (StringUtils.isBlank(text)) {
            return this.getByCondition(productQuery);
        }
        BaseResponse response = this.getByCondition(productQuery);
        List<ProductVO> productVOList = (List<ProductVO>) response.getData();
        productVOList = productVOList.stream().filter(productVO -> {
            String productName = productVO.getProductName();
            String brand = productVO.getBrand();
            String type = productVO.getType();
            if (productName.contains(text) || brand.contains(text) || type.contains(text)) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        return ResultUtils.success(productVOList);
    }

    @PostMapping("/update")
    @ApiOperation("修改产品")
    @AuthCheck(mustRole = "admin")
    public BaseResponse update(@RequestBody ProductDTO productDTO) {
        productService.updateProduct(productDTO);
        return ResultUtils.success(null);
    }

    /**
     * 修改状态出现问题
     *
     * @return
     */
    @PostMapping("/status")
    @ApiOperation(value = "单个或批量修改状态", notes = "只传id和修改后的status")
    @AuthCheck(mustRole = "admin")
    public BaseResponse status(@RequestBody Product product) {
        productService.updateById(product);
        return ResultUtils.success(null);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "单个或批量删除")
    @AuthCheck(mustRole = "admin")
    public BaseResponse delete(@RequestParam List<Integer> ids) {
        productService.removeBatchByIds(ids);
        return ResultUtils.success(null);
    }


}
